﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml;


namespace ClassLibrary
{
    public class CrossingoverLink
    {
        public string path { set; get; }
        public string type { set; get; }
        public string name { set; get; }
        public string path_to_chromosome_table { set; get; }

        // probability massive for locuses in chromosomes
        public int chromosome_pairs_amount;
        public Dictionary<int, List<double>> massive_probalities = new Dictionary<int, List<double>>();

        public bool available { set; get; }


        /* constructor */
        public CrossingoverLink(string path_to_crossingover_link)
        {
            path = path_to_crossingover_link;

            // open XML file            
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(File.ReadAllText(path_to_crossingover_link));

            // reading gene_table's attributes
            type = doc.DocumentElement.GetAttribute("Type");
            name = doc.DocumentElement.GetAttribute("Name");
            path_to_chromosome_table = doc.DocumentElement.GetAttribute("PathToChromosomeTable");
            chromosome_pairs_amount = Convert.ToInt32(doc.DocumentElement.GetAttribute("ChromosomePairsAmount"));

            if (chromosome_pairs_amount != 0)
            {
                int ch_id;
                double probability;
                string[] str_probabilities; 

                foreach (XmlNode xml_node in doc.GetElementsByTagName("chromosome"))
                {
                    List<double> locuses_probability = new List<double>();
                    ch_id = Convert.ToInt32(xml_node.Attributes[0].Value);                    

                    str_probabilities = xml_node.ChildNodes[0].InnerText.Split(' ');
                    for (int i = 0; i < str_probabilities.Count(); i++)
                    {
                        if (str_probabilities[i] != "")
                        {
                            probability = Convert.ToDouble(str_probabilities[i]);
                            locuses_probability.Add(probability);
                        }
                    }

                    massive_probalities.Add(ch_id, locuses_probability);
                }
            }
        }

        
        /* Method
         * Args: chromosome_pair_id, locus_id
         * Returns: probability of crossingover
        */ 
        public double getCrossingoverProbability(int chromosme_pair_id, int locus_id)
        {
            return massive_probalities[chromosme_pair_id][locus_id];
        }
    }
}
